<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Description of AgentController
 *
 * @author LeVinh
 */
include APPLICATION_PATH."/models/Admin_Area.php";
include APPLICATION_PATH."/models/Area_Province_Category.php";
include APPLICATION_PATH."/models/Agent.php";
include APPLICATION_PATH."/models/Agent_Category.php";
include APPLICATION_PATH."/models/Province.php";
include APPLICATION_PATH."/models/District.php";

class KinhDoanh_AgentController extends Zend_Controller_Action{
     public function init(){
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;                
        $this->AAmodelMapper = new Model_Admin_AreaMapper();
        $this->AACmodelMapper = new Model_Area_Province_CategoryMapper();
        $this->kinhdoanh_id = GlobalLib::getLoginId();
        $this->model = new Model_Agent();
        $this->modelMapper = new Model_AgentMapper();
        $this->AgentCatmodel= new Model_Agent_Category();
        $this->AgentCatmodelMapper= new Model_Agent_CategoryMapper();
        $this->Provincemodel= new Model_Province();
        $this->ProvincemodelMapper= new Model_ProvinceMapper();
        $this->Districtmodel= new Model_District();
        $this->DistrictmodelMapper= new Model_DistrictMapper();
    }
    public function indexAction()
    {         
    }
    public function addAction()
    {        
        if ($this->getRequest()->isPost()) {               
            $redirectUrl = $this->aConfig["site"]["url"] . "/kinhdoanh/agent/index";
            if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (isset($_POST["name"])) {
                $this->model->setName($_POST["name"]);
            }            
            if (isset($_POST["address"])) {
                $this->model->setAddress($_POST["address"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if (isset($_POST["district_id"])) {
                $this->model->setDistrict_Id($_POST["district_id"]);
            }
            if (isset($_POST["representavie"])) {
                $this->model->setRepresentavie($_POST["representavie"]);
            }
            if (isset($_POST["phone"])) {
                $this->model->setPhone($_POST["phone"]);
            }
            $this->model->setCharges(0);
            $this->model->setCharges_Sell(0);
            if (isset($_POST["agent_cat_id"])) {
                $this->model->setAgent_Cat_Id($_POST["agent_cat_id"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setArea_Id($this->AACmodelMapper->findareaidbyprovinceid($_POST["province_id"]));
            }
            if (isset($_POST["user_id"])) {
                $this->model->setUser_Id($_POST["user_id"]);
            }
             if (isset($_POST["agent_status"])) {
                $this->model->setAgent_Status($_POST["agent_status"]);
            }
            if (isset($_POST["order"])) {
                $this->model->setOrder($_POST["order"]);
            }            
            if (isset($_POST["status"])) {
                $status = 1;
            } else {
                $status = 0;
            }     
            if(isset($_POST["tax_code"]))
            {
                $this->model->setTax_Code($_POST["tax_code"]);
            }
            if(isset($_POST["identity_card"])){
                $this->model->setIdentity_Card($_POST["identity_card"]);
            }
            $this->model->setStatus($status);
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }
        $this->view->item = $this->model;
        $province_id= $this->model->getProvince_Id();
        if(empty($province_id)){
            $province_id=0;
        }
        if(empty($area_id)){
            $area_id=0;
        }        
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', 0, false, 'form-control', '', 'where id in (select area_id from admin_area where admin_id='.GlobalLib::getLoginId().')', '', 'onchange="getDistrict(\'' . $this->aConfig["site"]["url"] . 'kinhdoanh/service/index\',$(\'#province_id\').val(),\'district_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->districtHTML=GlobalLib::getComboByDistrict('district_id', 'district', 'id', 'name', 0, false, 'form-control', '', 'where province_id=' . $province_id);   
    }    
    public function editAction(){
        $id = $this->_getParam("id","");        
        $this->view->itemm=$id;
        $redirectUrl=$this->aConfig["site"]["url"]."kinhdoanh/agent/index";
        if(empty($id)){            
            $this->_redirect($redirectUrl);
        }
        $this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        if(empty($getId)){
            $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
          if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (isset($_POST["name"])) {
                $this->model->setName($_POST["name"]);
            }            
            if (isset($_POST["address"])) {
                $this->model->setAddress($_POST["address"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if (isset($_POST["district_id"])) {
                $this->model->setDistrict_Id($_POST["district_id"]);
            }
            if (isset($_POST["representavie"])) {
                $this->model->setRepresentavie($_POST["representavie"]);
            }
            if (isset($_POST["phone"])) {
                $this->model->setPhone($_POST["phone"]);
            }
            if (isset($_POST["charges"])) {
                $this->model->setCharges($this->model->getCharges());
            }
            if (isset($_POST["agent_cat_id"])) {
                $this->model->setAgent_Cat_Id($_POST["agent_cat_id"]);
            }
            if (isset($_POST["province_id"])) {
                $this->model->setArea_Id($this->AACmodelMapper->findareaidbyprovinceid($_POST["province_id"]));
            }
            if (isset($_POST["user_id"])) {
                $this->model->setUser_Id($_POST["user_id"]);
            }
            if (isset($_POST["agent_status"])) {
                $this->model->setAgent_Status($_POST["agent_status"]);
            }
            if (isset($_POST["order"])) {
                $this->model->setOrder($_POST["order"]);
            }            
            if (isset($_POST["status"])) {
                $status = 1;
            } else {
                $status = 0;
            } 
            if(isset($_POST["tax_code"]))
            {
                $this->model->setTax_Code($_POST["tax_code"]);
            }
            if(isset($_POST["identity_card"])){
                $this->model->setIdentity_Card($_POST["identity_card"]);
            }
            $this->model->setStatus($status);
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);          
        }
        $this->view->item=$this->model;
        $province_id= $this->model->getProvince_Id();
        if(empty($province_id)){
            $province_id=0;
        }
        if(empty($area_id)){
            $area_id=0;
        }
        $this->view->provinceHTML= GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->model->getProvince_Id(), false, 'form-control', '', 'where id in (select area_id from admin_area where admin_id='.GlobalLib::getLoginId().')', '', 'onchange="getDistrict(\'' . $this->aConfig["site"]["url"] . 'kinhdoanh/service/index\',$(\'#province_id\').val(),\'district_id\',\'Xin Chờ Trong Giây Lát\');"');
        $this->view->districtHTML=GlobalLib::getComboByDistrict('district_id', 'district', 'id', 'name', $this->model->getDistrict_Id(), false, 'form-control', '', 'where province_id=' . $province_id);   
    }
    public function serviceAction() {   
        foreach ($this->modelMapper->getAgentByAdminId($this->kinhdoanh_id) as $items)
        {
            if($items['district_name']=="0"){
                $district_name="Không có";
            } else {
                $district_name=$items['district_name'];
            }
            if($items['agent_cat_name']=="0"){
                $agent_cat_name="Không Có";
            } else {
                $agent_cat_name= $items['agent_cat_name'];
            }
            if($items['province_name']=="0"){
                $province_name="Không Có";
            } else {
                $province_name=  $items['province_name'];
            }
            $menber[] = array(
                        'id' => $items['agent_id'],
                        'name' => $items['agent_name'],
                        'address' => $items['address'],
                        'province_name' => $province_name,
                        'district_name' => $district_name,
                        'agent_category_name' => $agent_cat_name
                    );
        }
        echo json_encode($menber);
        exit();
    }
    public function checkagentnameAction(){
        $this->_helper->layout->disableLayout();
          if($this->_request->isPost()){
            $agent_name=$this->_getParam("agent_name","");
            $id=$this->modelMapper->checkAgentName($agent_name);
            if($id !=0){
                 $menber[]=array(
                     'code'=>1,
                     'message'=>''
                         );  
             } else {
                  $menber[]=array(
                     'code'=>0,
                     'message'=>''
                         );  
             }
             echo json_encode($menber);
             exit();
        }
    }
}
